﻿using UnityEngine;
using System.Collections;
public class TweenBase:MonoBehaviour{
	public static void PlayAllTween(GameObject target){
		TweenBase[] tween = target.GetComponentsInChildren<TweenBase> ();
		if (tween != null) {
			for (int i = 0; i < tween.Length; i++) {
				tween [i].Play ();
			}
		}
	}

	public float _time;
	protected float _savetime;
	protected bool _Play;
	public bool _autoPlay;
	void Start(){
		Init ();
		if (_autoPlay) {
			Play ();
		}
	}

	public void Play(){
		_savetime = Time.time;
		_Play = true;
		PlayRest ();
	}

	protected virtual void Update(){
		if (_Play) {
			if (Time.time - _savetime >= _time) {
				_Play = false;
				PlayFinish ();
			}
			Lerp ();
		}
	}
	protected virtual void Lerp(){
		
	}
	protected virtual void Init(){

	}
	protected virtual void PlayFinish(){
		
	}
	protected virtual void PlayRest (){}
}
public class TweenMove : TweenBase {
	public Vector3 _startPos;
	public Vector3 _endPos;

	public bool _local = true;
	public Transform _cachedTrasnform;

	// Use this for initialization
	protected override void Init () {
		_cachedTrasnform = transform;
	}

	protected override void Lerp(){
		Vector3 ve3 = Vector3.Lerp (_startPos, _endPos, (Time.time - _savetime) / _time);
		if (_local) {
			_cachedTrasnform.transform.localPosition = ve3;
		} else
			_cachedTrasnform.transform.position = ve3;
	}

	protected override void PlayRest(){
		if (_local) {
			_cachedTrasnform.transform.localPosition = _startPos;
		} else
			_cachedTrasnform.transform.position = _startPos;
	}
}
